<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
</head>
<body>
    <div id="root">
        <input type="text" placeholder="输入名字关键字进行筛选" v-model="name">
      <h1 v-for="s in studentsF">
          我是{{s.name}},今年{{s.age}}岁
      </h1>
    </div>
</body>
<script>
    Vue.config.productionTip = false;
    const vm = new Vue({
        el:'#root',
        data:{
            name:'',
            students:[
               {
                   name:"张三",
                   age:'18'
               },
               {
                   name:'王麻子',
                   age:'86'
               },{
                   name:"李磊",
                   age:'53'
               }
           ],
           studentsF:[]
        },
        watch:{
            name:{
                // 空字符串用indexOf对比任何字符串都是0，所以不等于-1，一开始就对比一下，就会获得所有数据
                immediate:true,
                handler(newMsg){
                    this.studentsF = this.students.filter((p)=>{
                    return p.name.indexOf(newMsg) !==-1;
                })
                }
            }
        }
    })
</script>
</html>